package com.haixiaoke.saas.repository.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.repository.mapper.RecommendGroupMapper;
import com.haixiaoke.saas.repository.mapper.RecommendItemMapper;
import com.haixiaoke.saas.repository.pojo.HousingAllocation;
import com.haixiaoke.saas.repository.pojo.RecommendGroup;
import com.haixiaoke.saas.repository.pojo.RecommendItem;
import com.haixiaoke.saas.repository.service.IApartmentService;
import com.haixiaoke.saas.repository.service.IRecommendGroupService;
import com.haixiaoke.saas.repository.vo.ApartmentDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 推荐房源组Service业务层处理
 *
 * @author 志平
 * @date 2023-04-18
 */
@Service
public class RecommendGroupServiceImpl implements IRecommendGroupService {
    @Autowired
    private RecommendGroupMapper recommendGroupMapper;
    @Autowired
    private RecommendItemMapper recommendItemMapper;
    @Autowired
    private IApartmentService apartmentService;

    /**
     * 查询推荐房源组
     *
     * @param groupId 推荐房源组主键
     * @return 推荐房源组
     */
    @Override
    public RecommendGroup selectRecommendGroupByGroupId(Long groupId) {
        return recommendGroupMapper.selectRecommendGroupByGroupId(groupId);
    }

    /**
     * 查询推荐房源组列表
     *
     * @param recommendGroup 推荐房源组
     * @return 推荐房源组
     */
    @Override
    public List<RecommendGroup> selectRecommendGroupList(RecommendGroup recommendGroup) {
        List<RecommendGroup> groups = recommendGroupMapper.selectRecommendGroupList(recommendGroup);
        for (RecommendGroup group : groups) {
            Long groupId = group.getGroupId();
            List<RecommendItem> items = recommendItemMapper.selectRecommendItemByGroupId(groupId);
            for (RecommendItem item : items) {
                String houseId = item.getHouseId();
                if (StrUtil.isNotBlank(houseId)) {
                    ApartmentDetailVo detail = apartmentService.getDetail(houseId);
                    if (ObjectUtil.isNotEmpty(detail)) {
                        BeanUtil.copyProperties(detail.getHousingAllocation(), item);
                    } else {
                        recommendItemMapper.deleteRecommendItemByItemId(item.getItemId());
                    }
                }
            }
            items = items.stream().filter(x -> x.getAllocationId() != null && HousingAllocation.APARTMENT_STATE_UP.equals(x.getApartmentState())).collect(Collectors.toList());
            group.setItems(items);
        }
        return groups;
    }

    /**
     * 新增推荐房源组
     *
     * @param recommendGroup 推荐房源组
     * @return 结果
     */
    @Override
    public int insertRecommendGroup(RecommendGroup recommendGroup) {
        recommendGroup.setCreateTime(DateUtil.date());
        return recommendGroupMapper.insertRecommendGroup(recommendGroup);
    }

    /**
     * 修改推荐房源组
     *
     * @param recommendGroup 推荐房源组
     * @return 结果
     */
    @Override
    public int updateRecommendGroup(RecommendGroup recommendGroup) {
        recommendGroup.setUpdateTime(DateUtil.date());
        return recommendGroupMapper.updateRecommendGroup(recommendGroup);
    }

    /**
     * 批量删除推荐房源组
     *
     * @param groupIds 需要删除的推荐房源组主键
     * @return 结果
     */
    @Override
    public int deleteRecommendGroupByGroupIds(Long[] groupIds) {
        return recommendGroupMapper.deleteRecommendGroupByGroupIds(groupIds);
    }

    /**
     * 删除推荐房源组信息
     *
     * @param groupId 推荐房源组主键
     * @return 结果
     */
    @Override
    public int deleteRecommendGroupByGroupId(Long groupId) {
        return recommendGroupMapper.deleteRecommendGroupByGroupId(groupId);
    }
}
